package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.forecast.bean.WeatherCode;
import com.srit.system.bean.GridAttribute;
import com.srit.system.dao.GridAttributeDao;

@Service
public class GridAttributeDaoImpl implements GridAttributeDao {
	@Autowired(required = true)
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(GridAttribute gridAttribute) {
		String sql = "insert into TC_GRIDATTRIBUTE (id,showType,codeType,name) values(SEQ_TC_GRIDATTRIBUTE.NEXTVAL,?,?,?)";
		jdbcTemplate.update(sql, new Object[] { gridAttribute.getShowType(),
				gridAttribute.getCodeType(), gridAttribute.getName() });
	}

	@Override
	public void update(GridAttribute gridAttribute) {
		String sql = "update  TC_GRIDATTRIBUTE set showType=?,codeType=?,name=? where id=?";
		jdbcTemplate.update(sql, new Object[] { gridAttribute.getShowType(),
				gridAttribute.getCodeType(), gridAttribute.getName(),
				gridAttribute.getId() });
	}

	@Override
	public GridAttribute getById(Integer id) {
		String sql = "select * from TC_GRIDATTRIBUTE where id=?";

		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new GridAttributeRowMapper());
	}

	@Override
	public void page(Page page) {
		String sqlCount = "select count(*) from TC_GRIDATTRIBUTE where 1=1 ";
		String sqlAnd = " ";
		String sqlSelect = "select  t2.id,t2.showType,t2.codeType,t2.name "
				+ "from (select rownum r,t1.* from TC_GRIDATTRIBUTE t1 where rownum<? "
				+ sqlAnd + ") t2 where t2.r>?";
		int count = jdbcTemplate.queryForInt(sqlCount + sqlAnd);
		List<WeatherCode> list = jdbcTemplate.query(sqlSelect, new Object[] {
				(page.getPageNum() + 1) * page.getPageSize(),
				page.getPageNum() * page.getPageSize() },
				new GridAttributeRowMapper());
		page.setCount(count);
		page.setList(list);
	}

	@Override
	public int getMaxId() {
		String sql = "select max(id) from TC_GRIDATTRIBUTE";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<GridAttribute> listAll() {
		String sql = "select * from TC_GRIDATTRIBUTE";
		return jdbcTemplate.query(sql, new GridAttributeRowMapper());
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}

}

class GridAttributeExtractor implements ResultSetExtractor {

	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		GridAttribute ga = new GridAttribute();
		ga.setId(rs.getInt("ID"));
		ga.setName(rs.getString("NAME"));
		ga.setCodeType(rs.getString("CODETYPE"));
		ga.setShowType(rs.getString("SHOWTYPE"));
		return ga;
	}
}

class GridAttributeRowMapper implements RowMapper {

	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		GridAttributeExtractor extractor = new GridAttributeExtractor();
		return extractor.extractData(rs);
	}

}
